package com.jfs.strategy.message.impl;

import com.jfs.enums.MessageEnum;
import com.jfs.envent.EventPublisher;
import com.jfs.envent.entity.MessageConfigDownload;
import com.jfs.observer.request.abstracts.AbstractProcessor;
import com.jfs.strategy.message.MessageStrategy;
import com.jfs.utils.XstreamUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.sip.RequestEvent;

/**
 * @Description 设备配置信息策略处理类
 * @E-mail cwm3412638@163.com
 * @Author cwm
 * @Date 2022/8/1
 */
@Component
@AllArgsConstructor
@Slf4j
public class MessageConfigDownloadStrategy implements MessageStrategy {
    private final EventPublisher eventPublisher;
    @Override
    public boolean support(String content) {
        return content.contains(MessageEnum.MESSAGE_CONFIG_DOWNLOAD.getCmd());
    }

    @Override
    public void processor(String content, AbstractProcessor abstractProcessor, RequestEvent requestEvent) {
        try {
            MessageConfigDownload messageConfigDownload = XstreamUtil.toBean(MessageConfigDownload.class, content);
            messageConfigDownload.setRequestEvent(requestEvent);
            messageConfigDownload.setIp(abstractProcessor.getIpByRequest(requestEvent));
            messageConfigDownload.setPort(abstractProcessor.getPortFromRequest(requestEvent));
            messageConfigDownload.setTransport(abstractProcessor.getTransactionFormRequest(requestEvent));
            //发布事件
            eventPublisher.publish(messageConfigDownload);
            //回复ok
            abstractProcessor.doSuccess(requestEvent);
        }catch (Exception e){
            log.info("设备配置信息处理发生异常:{}",e.getMessage());
            e.printStackTrace();
        }
    }
}
